package com.shujia.scala

import scala.io.Source

object Demo30Student {
  def main(args: Array[String]): Unit = {

    /**
      *
      * 4、统计偏科最严重的前100名学生  [学号，姓名，班级，科目，分数]
      *
      */

    val scores = Source.fromFile("scala/data/score.txt").getLines().toList
    val cources = Source.fromFile("scala/data/cource.txt").getLines().toList
    //科目总分
    val cSumScoreKV: Map[String, Int] = cources.map(line => {
      val split = line.split(",")
      (split(0), split(2).toInt)
    }).toMap


    /**
      * 计算每个学生所有分数的方差
      * 由于每科总分不一样   所以需要将每一科的分数拉取同一个水平（归一化，标准化）
      */


    //1、对数据进行归一化   每一科的分数除以每一科的总分
    val yiScore = scores.map(line => {
      val split = line.split(",")
      val id = split(0)
      val courceId = split(1)
      val score = split(2).toInt

      val sumScore = cSumScoreKV.getOrElse(courceId, 100)

      //归一化
      val yi = score / sumScore.toDouble

      (id, yi)
    })

    //计算方差   每个学生单独计算
    val groupByScore = yiScore.groupBy(kv => kv._1).toList

    val stdScore = groupByScore.map(kv => {
      val id = kv._1

      //每个学生归一化后的分数
      val score = kv._2.map(_._2)

      //计算均值
      val mean = score.sum / score.size

      //计算方差分子
      val sum = score.map(d => (d - mean) * (d - mean)).sum

      val std = sum / score.size

      (id, std)
    })


    //排序 降序排序   方差越大偏科越严重
    val top100 = stdScore.sortBy(kv => -kv._2).take(100).map(kv => kv._1)

    //关联分数表
    val result = scores.filter(line => {
      val id = line.split(",")(0)
      top100.contains(id)
    })


    result.foreach(println)
  }

}
